package uk.ac.nott.cs.g53dia;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import utils.ControlPanel;
import utils.Simulation;

/**
 * A simple user interface for watching an individual bee.
 * 
 * @author Neil Madden.
 */
/*
 * Copyright (c) 2003 Stuart Reeves Copyright (c) 2003-2005 Neil Madden
 * (nem@cs.nott.ac.uk).
 * 
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */
public class BeeViewer extends JFrame {
	BeeViewerIconFactory iconfactory;
	JLabel[][] cells, bees;
	JLabel tstep, energy, pos, eggs;
	JComboBox beeList;
	JButton pause;
	List<Bee> swarm;
	Bee bee;
	final static int SIZE = Bee.VIEW_RANGE * 2 + 1;

	public BeeViewer(List<Bee> swarm) {
		this(swarm, new DefaultBeeViewerIconFactory());
	}

	public BeeViewer(List<Bee> swarm, BeeViewerIconFactory fac) {
		this.swarm = swarm;
		this.bee = swarm.get(0);
		this.iconfactory = fac;
		Container c = getContentPane();
		c.setLayout(new BorderLayout());

		String[] beeNames = new String[swarm.size()];
		for (int i = 0; i < swarm.size(); i++) {
			beeNames[i] = "Bee " + i;
		}

		beeList = new JComboBox(beeNames);
		// Create the cell viewer
		cells = new JLabel[SIZE][SIZE];
		bees = new JLabel[SIZE][SIZE];
		JLayeredPane lp = new JLayeredPane();
		JPanel p = new JPanel(new GridLayout(SIZE, SIZE));
		JPanel beePanel = new JPanel(new GridLayout(SIZE, SIZE));
		p.setBackground(new Color(190, 225, 190));

		for (int y = 0; y < SIZE; y++) {
			for (int x = 0; x < SIZE; x++) {
				cells[x][y] = new JLabel();
				p.add(cells[x][y]);
				bees[x][y] = new JLabel();
				beePanel.add(bees[x][y]);
			}
		}
		lp.add(p, new Integer(0));
		beePanel.setOpaque(false);
		p.setBounds(0, 0, 420, 420);
		lp.add(beePanel, new Integer(1)); // Add above the background
		beePanel.setBounds(0, 0, 420, 420);
		// Create a bee label
		JLabel beelabel = new JLabel(iconfactory.getIconForBee2(bee));
		lp.add(beelabel, new Integer(1)); // Add above the background
		lp.setSize(new Dimension(420, 420));
		beelabel.setBounds(195, 195, 30, 30);
		c.add(lp, BorderLayout.CENTER);

		// Create some labels to show info about the bee and environment
		JPanel infop = new JPanel(new GridLayout(2, 4));
		infop.add(new JLabel("Timestep:"));
		tstep = new JLabel("0");
		infop.add(tstep);
		infop.add(new JLabel("Energy:"));
		energy = new JLabel("200");
		infop.add(energy);
		infop.add(new JLabel("Position:"));
		pos = new JLabel("(0,0)");
		infop.add(pos);
		infop.add(new JLabel("Eggs Laid:"));
		eggs = new JLabel("0");
		infop.add(eggs);
		infop.add(beeList);
		beeList.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bee = BeeViewer.this.swarm.get(beeList.getSelectedIndex());
			}
		});

		c.add(infop, BorderLayout.SOUTH);
		// infop.setPreferredSize(new Dimension(200,100));

		setSize(420, 470);
		setTitle("Bee Viewer");
		setVisible(true);
		addWindowListener(new WindowListener() {
			public void windowActivated(WindowEvent e) {}
			public void windowClosed(WindowEvent e) {}
			public void windowDeactivated(WindowEvent e) {}
			public void windowDeiconified(WindowEvent e) {}
			public void windowIconified(WindowEvent e) {}
			public void windowOpened(WindowEvent e) {}
			public void windowClosing(WindowEvent e) {
				ControlPanel.getInstance().setStartEnabled();
				Simulation.setStarted(false);
			}
		});
	}

	public void setSwarm(List<Bee> swarm) {
		this.swarm = swarm;
	}

	public void tick(Environment env) {
		Cell[][] view = env.getView(bee.getPosition(), Bee.VIEW_RANGE);
		pos.setText(bee.getPosition().toString());
		tstep.setText(new String("" + env.getTimestep()));
		eggs.setText(new String("" + bee.eggs_laid));
		energy.setText(new String("" + bee.getEnergy()));
		for (int x = 0; x < SIZE; x++) {
			for (int y = 0; y < SIZE; y++) {
				// if (x == bee.getPosition().x && y == bee.getPosition().y) {
				// continue;
				// }
				Icon cur = iconfactory.getIconForCell(view[x][y], swarm);
				cells[x][y].setIcon(cur);
				bees[x][y].setIcon(null);
			}
		}
		addBees(env, bee.getPosition(), Bee.VIEW_RANGE);
	}

	private void addBees(Environment env, Point position, int viewRange) {
		for (Point point : env.getVisibleBees(position, viewRange)) {
			bees[Bee.VIEW_RANGE + (point.x - position.x)][Bee.VIEW_RANGE
					- (point.y - position.y)].setIcon(iconfactory
					.getIconForBee(bee));
		}
	}
}
